/* global angular */
(function () {
    angular.module('WingTrainer', [])
	.constant('ShipConst', {
        SIZE: 20
    })
}());



/* global angular */
(function () {
    angular.module('WingTrainer').controller('MainCtrl', function ($scope) {
	console.log('main controller');
	  var ships = [
    {
      "id": 0,
      "position": {
        "x": 25,
        "y": 25,
        "r": 180
      }
    },
    {
      "id": 1,
      "position": {
        "x": 75,
        "y": 25,
        "r": 180
      }
    },
    {
      "id": 2,
      "position": {
        "x": 525,
        "y": 575,
        "r": 0
      }
    },
    {
      "id": 3,
      "position": {
        "x": 575,
        "y": 575,
        "r": 0
      }
    }
  ]; 
  
  var movementRules=[
  {name:'Turn Left 2',position:{x:0,y:0,length:10,arc:45}}
  ];
  
  
  $scope.model={ships:ships,movementRules:movementRules};
    
});
})();

/* global angular */
(function () {
    angular.module('WingTrainer').directive('playzone', function (ShipConst) {
	return{
			restrict: 'A',
			scope:{gameModel:'=playzone'},
            controller: function ($scope, $element) {
                console.log('controller for PlayZone');
				
				$scope.drawShipShape=function(){
				 var shape = new createjs.Shape();
				 shape.graphics.beginStroke("#000000").drawRect(0, 0, ShipConst.SIZE,ShipConst.SIZE).lt(ShipConst.SIZE/2,ShipConst.SIZE/2).lt(ShipConst.SIZE,0);
				 return shape;
				};
							
				$scope.initialisePlayZone=function(){
					
					console.log('initialise play zone...');			


					stage = new createjs.Stage("canvas");
					dragContainer = new createjs.Container();
					stage.addChild(dragContainer);

					console.log('adding ships...');			
					
					$scope.shipShapes = [];
					for(var shipN in $scope.gameModel.ships){
					var shipModel = $scope.gameModel.ships[shipN];
					console.log('shipModel');
					console.log(shipModel);
					var shipShape = $scope.drawShipShape();				
					$scope.shipShapes[shipN]=shipShape;
					shipShape.setTransform(shipModel.position.x,shipModel.position.y,1,1,shipModel.position.r);					
					dragContainer.addChild(shipShape);				
					}
					console.log('...ships added');			
					
					stage.update();				
				};
				
				$scope.moveShapeToPosition=function(shipShape,newPosition){				
					shipShape.setTransform(newPosition.x,newPosition.y,1,1,newPosition.r);					
					//console.log('changed ship position');
					stage.update();	
				};

            },
			link: function (scope, element, attrs) {
                console.log('link PlayZone instance');			
				
                
				
				scope.initialisePlayZone();
				
				scope.$watch('gameModel.ships',function(a,b){
					for(var shipN in scope.gameModel.ships){
						var shipShape = scope.shipShapes[shipN];
						var newPosition = scope.gameModel.ships[shipN].position;
						scope.moveShapeToPosition(shipShape,newPosition);				
						scope.moveShapeToPosition(shipShape,newPosition);									
					}
				},true);
				
				
				function drawMovementTemplate(){
				
					var tmpl = scope.gameModel.movementRules[0];
					console.log(tmpl);
				
					var ruleShape = new createjs.Shape();
					var quarter = Math.PI/2;					
					ruleShape.graphics.beginStroke("#000000").arc(scope.gameModel.ships[3].position.x,scope.gameModel.ships[3].position.y, 50, quarter,2*quarter);
					dragContainer.addChild(ruleShape);
					stage.update();	
					
				};
				
				drawMovementTemplate();
				
            }
	
	};
});
})();
